import { defineStore } from 'pinia'
import { studentApi } from '@/api/student'

export const useStudentStore = defineStore('student', {
  state: () => ({
    student: null,
    loading: false
  }),

  actions: {
    async fetchStudentInfo() {
      try {
        this.loading = true
        const response = await studentApi.getInfo()
        this.student = response.data
        return response
      } catch (error) {
        this.student = null
        throw error
      } finally {
        this.loading = false
      }
    },

    async createStudent(data) {
      try {
        const response = await studentApi.create(data)
        // 创建成功后重新获取学生信息
        await this.fetchStudentInfo()
        return response
      } catch (error) {
        throw error
      }
    },

    async updateStudentInfo(data) {
      try {
        const response = await studentApi.updateInfo(data)
        if (this.student) {
          this.student = { ...this.student, ...data }
        }
        return response
      } catch (error) {
        throw error
      }
    }
  }
})